﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;

namespace ECatalog.Models.Helpers
{
    public class ImageHelper
    {
        public static Image ResizeImage(Image imgToResize, System.Drawing.Size size)
        {
            var sourceWidth = imgToResize.Width;
            var sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float) size.Width/(float) sourceWidth);
            nPercentH = ((float) size.Height/(float) sourceHeight);

            nPercent = nPercentH < nPercentW ? nPercentH : nPercentW;

            var destWidth = (int) (sourceWidth*nPercent);
            var destHeight = (int) (sourceHeight*nPercent);

            var b = new Bitmap(destWidth, destHeight);
            var g = Graphics.FromImage((Image) b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image) b;
        }

        public static Image CropImage(Image img, Rectangle cropArea)
        {
            var bmpImage = new Bitmap(img);
            var bmpCrop = bmpImage.Clone(cropArea,
                                         bmpImage.PixelFormat);
            return (Image) (bmpCrop);
        }

        public static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            var codecs = ImageCodecInfo.GetImageEncoders();
            return codecs.FirstOrDefault(t => t.MimeType == mimeType);
        }

        public static void CreateDirectory(string path)
        {
            Directory.CreateDirectory(path);
        }

        public static void BuildImage(HttpPostedFileBase file, string pathFull, string fileNamePath, int fW, int fH)
        {
            var fullSize = new System.Drawing.Size(fW, fH);

            Image image = new Bitmap(file.InputStream);

            var fullImage = ResizeImage(image, fullSize);
            SaveJpeg(pathFull + fileNamePath, (Bitmap) fullImage, 85);
        }

        public static void SaveJpeg(string path, Bitmap img, long quality)
        {
            var qualityParam = new EncoderParameter(Encoder.Quality, quality);

            var jpegCodec = GetEncoderInfo("image/jpeg");

            if (jpegCodec == null) return;

            var encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;

            img.Save(path, jpegCodec, encoderParams);
        }

        public static string GetImageType(string contentType)
        {
            if (contentType.Equals("image/jpeg"))
            {
                return ".jpg";
            }
            if (contentType.Equals("image/png"))
            {
                return ".png";
            }
            if (contentType.Equals("image/gif"))
            {
                return ".gif";
            }
            return ".xxx";
        }
    }
}